package com.qs.rocketmq.sc.controller;

import com.alibaba.fastjson.JSON;
import com.qs.rocketmq.sc.dto.MessageDTO;
import com.qs.rocketmq.sc.service.MySource;
import org.apache.rocketmq.common.message.MessageConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping(value = "/mqTest")
public class ScMQTestController {

    private static Logger logger = LoggerFactory.getLogger(ScMQTestController.class);

    @Autowired
    private MySource mySource;

    @GetMapping("/sendTx")
    public boolean sendTx() {
        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setId(new Random().nextInt() + "");
        Args args = new Args();
        args.setArgs1(1);
        args.setArgs2(2);
        Message<MessageDTO> message = MessageBuilder
                .withPayload(messageDTO)
                .setHeader("args", JSON.toJSONString(args))
                .build();
        return mySource.output().send(message);
    }

    public static class Args{
        private Integer args1;
        private Integer args2;

        public Integer getArgs1() {
            return args1;
        }

        public void setArgs1(Integer args1) {
            this.args1 = args1;
        }

        public Integer getArgs2() {
            return args2;
        }

        public void setArgs2(Integer args2) {
            this.args2 = args2;
        }

        @Override
        public String toString() {
            return "Args{" +
                    "args1=" + args1 +
                    ", args2=" + args2 +
                    '}';
        }
    }



    @GetMapping("/sendTag")
    public boolean sendTag() {
        List<String> tags = Arrays.asList("tengxun", "youku", "tudou");
        for (String tag : tags) {
            // 构建消息对象
            MessageDTO messageDTO = new MessageDTO();
            // 这里id使用随机数
            messageDTO.setId(String.valueOf(new Random().nextInt(100)));
            messageDTO.setMsg("hello rocketMQ");

            logger.info("发送端，tag={}", tag);
            // 创建spring message对象
            Message<MessageDTO> springMessage = MessageBuilder
                    .withPayload(messageDTO)
                    //设置tag
                    .setHeader(MessageConst.PROPERTY_TAGS, tag)
                    .build();
            mySource.output().send(springMessage);
        }

        return true;
    }

    /**
     * 测试 发送消息
     * 访问url：http://localhost:8090/mqTest/send
     *
     * @author qinyupeng
     * @since 2021-9-11 15:30:53
     */
    @GetMapping("/send")
    public boolean send() {
        // 构建消息对象
        MessageDTO messageDTO = new MessageDTO();
        // 这里id使用随机数
        messageDTO.setId("1");
        messageDTO.setMsg("hello rocketMQ");

        // 创建spring message对象
        Message<MessageDTO> springMessage = MessageBuilder.withPayload(messageDTO).build();
        logger.info("发送端线程id={}", Thread.currentThread().getId());
        return mySource.output().send(springMessage);
    }

    /**
     * 测试 定时消息
     * 访问url：http://localhost:8090/mqTest/sendDelay
     *
     * @author qinyupeng
     * @since 2021-9-11 15:30:53
     */
    @GetMapping("/sendDelay")
    public boolean sendDelay() {
        // 构建消息对象
        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setId(UUID.randomUUID().toString());
        messageDTO.setMsg("hello rocketMQ");

        // 创建spring message对象
        Message<MessageDTO> springMessage = MessageBuilder
                .withPayload(messageDTO)
                //设置延迟级别为等级3，延迟10s后消费
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, "3")
                .build();
        return mySource.output().send(springMessage);
    }
}
